<?php
/**
 *    财务退款管理控制器
 */
class Commission_accountApp extends BackendApp
{
    protected $_commission_account_mod;
    protected $_commission_detail_mod;

    function __construct()
    {
        $this->Commission_AccountApp();
    }

    function Commission_AccountApp()
    {
        parent::BackendApp();
        $this->_commission_account_mod =& m('commission_account');
        $this->_commission_detail_mod = & m('commission_detail');
    }

    function index(){
        $page = $this->_get_page();
        $conditions = $this->_get_query_conditions(array(
            array(
                'field' => 'status',
                'equal' => '=',
                'type'  => 'numeric',
            ),
            array(
                'field' => 'commission_account.account_no',       //按用户名,店铺名,支付方式名称进行搜索
                'equal' => 'LIKE',
                'name'  => 'account_no',
            ),
        ));
        if($_GET['user_id']){
            $conditions .= ' AND commission_account.user_id = '.$_GET['user_id'];
            $this->assign('user_id',$_GET['user_id']);
        }

        if($_GET['add_time_from']){
            $from_time = strtotime($_GET['add_time_from']);
            $conditions = ' AND start_time >='.$from_time;
            if($_GET['add_time_to']){
                $to_time =strtotime($_GET['add_time_to']);
                $conditions = ' AND start_time >='.$from_time.' AND end_time <='.$to_time;
            }
        }elseif($_GET['add_time_to']){
            $to_time =strtotime($_GET['add_time_to']);
            $conditions = ' AND end_time <='.$to_time;
        }
        $this->assign('add_time_from',$_GET['add_time_from']);
        $this->assign('add_time_to',$_GET['add_time_to']);
        $res = $this->_commission_account_mod->find(array(
            'fields' => 'commission_account.*,member.user_name,member.phone_mob',
            'join' => 'has_member_commission',
            'conditions'    => '1=1 ' . $conditions,
            'limit' => $page['limit'],
            'count' => true,
            'order' => "start_time desc"
        ));
        foreach($res as $k=>$v){
            //定义状态
            $res[$k]['status_value'] = Commission_accountModel::$statusMap[$v['status']];
        }
        //查询推广人
        $memberModel = &m('member');
        $memberInfo = $memberModel->find(array("conditions"=>"is_promoters = 1"));

        $this->assign('commission_account_status_list', array(
            1  =>  Commission_accountModel::$statusMap[1],
            2  =>  Commission_accountModel::$statusMap[2],
            3  =>  Commission_accountModel::$statusMap[3],
        ));
        $this->assign('commission_account_user_list', $memberInfo);
        $page['item_count'] = $this->_commission_account_mod->getCount();   //获取统计的数据
        $this->_format_page($page);
        $this->assign('res', $res);
        $this->assign('filtered', $conditions? 1 : 0); //是否有查询条件
        $this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
            'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
        $this->assign('page_info', $page);
        $this->display('commission_account.index.html');

    }


    //审核通过账单审核
    function operation(){
        $id = $_POST['id'];
        $msg = '参数错误';
        if($id && is_numeric($id)){
            $info = $this->_commission_account_mod->get(array("conditions"=>"id = ".$id));
            $msg = '信息错误';
            if(!empty($info)){
                $msg = '账单状态错误';
                if($info['status'] == 1){
                    $saveData['confirmation_time'] = time();
                    $saveData['status'] = 2;
                    $save = $this->_commission_account_mod->edit($id,$saveData);
                    $msg = '操作失败';
                    if($save){
                        $msg = '操作成功';
                    }
                }
            }
        }
        echo json_encode(array('msg'=>$msg));
        exit();
    }
    //批量审核通过
    function operations(){
        $id = $_GET['id']?trim($_GET['id']):'';

        if(!$id){
            $this->show_warning('参数错误');
            return false;
        }
        $ids = explode(',',$id);
        if(!empty($ids)){
            $time = time();
            foreach($ids as $k=>$v){
                $info = $this->_commission_account_mod->get(array("conditions"=>"id = ".$v));
                if($info['status'] == 1){
                    $saveData['status'] = 2;
                    $saveData['confirmation_time'] = $time;
                    $save = $this->_commission_account_mod->edit($v,$saveData);
                }else{
                    continue;
                }
            }
        }
        $this->show_message('操作成功！', 'back_list', 'index.php?app=commission_account&act=index&page=' . $_GET['ret_page']);
    }



    //导出功能
    function excel(){
        $id = $_GET['id']?trim($_GET['id']):$_GET['id'];
        if(!$id){
            $this->show_warning('参数错误');
            return false;
        }
        $res = $this->_commission_detail_mod->find(array('conditions'=>'account_id = '.$id));
        foreach($res as $k=>$v){
            $res[$k]['order_pay_time_value'] = '未付款';
            if($v['order_pay_time'] > 0){
                $res[$k]['order_pay_time_value'] = date('Y-m-d H:i:s',$v['order_pay_time']);
            }
            $res[$k]['order_finish_time_value'] = '未完成';
            if($v['order_finish_time'] > 0){
                $res[$k]['order_finish_time_value'] = date('Y-m-d H:i:s',$v['order_finish_time']);
            }
            $res[$k]['commission_finish_time_value'] = date('Y-m-d H:i:s',$v['commission_finish_time']);
            $res[$k]['commission_proportion_value'] = $v['commission_proportion'] * 100;
        }

        $to_charset = (CHARSET == 'utf-8') ? substr(LANG, 0, 2) == 'sc' ? 'gbk' : 'big5' : '';
        //格式化type 、status  字段
        $num = 0;
        $data[$num]['order_sn'] = '订单号';
        $data[$num]['order_pay_time_value'] = '订单收款日期';
        $data[$num]['goods_id'] = '产品编码';
        $data[$num]['goods_name'] = '产品名称';
        $data[$num]['num'] = '数量';
        $data[$num]['price'] = '产品单价';
        $data[$num]['price_amount'] = '总金额(不含税不运费)';
        $data[$num]['order_finish_time_value'] = '订单成交日期';
        $data[$num]['commission_proportion_value'] = '服务费%';
        $data[$num]['sign'] = '正负值';
        $data[$num]['commission'] = '服务费';
        $data[$num]['stock_style'] = '销售方式';
        $data[$num]['user_name'] = '业务员';
        $data[$num]['serial_number'] = '业务员代码';
        foreach($res as $k=>$v){
            $data[$num+1]['order_sn']                    = $v['order_sn'];
            $data[$num+1]['order_pay_time_value']        = $v['order_pay_time_value'];
            $data[$num+1]['goods_id']                    = $v['goods_id'];
            $data[$num+1]['goods_name']                  = $v['goods_name'];
            $data[$num+1]['num']                         = $v['num'];
            $data[$num+1]['price']                       = '￥'.$v['price'];
            $data[$num+1]['price_amount']                = '￥'.$v['price_amount'];
            $data[$num+1]['order_finish_time_value']     = $v['order_finish_time_value'];
            $data[$num+1]['commission_proportion_value'] = $v['commission_proportion_value'].'%';
            $data[$num+1]['sign']                        = $v['sign'];
            $data[$num+1]['commission']                  = $v['commission'];
            $data[$num+1]['stock_style']                 = $v['stock_style'];
            $data[$num+1]['user_name']                   = $v['user_name'];
            $data[$num+1]['serial_number']               = $v['serial_number'];
            $num++;
        }
        if(!empty($res)){
            $this->export_to_csv($data, date('Y-m-d H:i:s').'账单信息', $to_charset);
            exit();
        }
        $this->show_warning('数据为空！');
        return false;
    }


    //打款
    function makeMoney(){
        echo json_encode(array("msg"=>"功能暂未开通"));
        exit();
        $res = $this->_commission_account_mod->makeModel();
    }



    //查看账单详情
    function detail(){
        $id = $_POST['id'];
        $code = -1;
        $msg = '参数错误';
        $data = array();
        if($id && is_numeric($id)){
            $msg = '账单信息错误';
            $commission_accountInfo = $this->_commission_account_mod->get(array("conditions"=>"id = ".$id));
            if(!empty($commission_accountInfo)){
                $msg = '账单信息为空';
                $commission_account_detailInfo = $this->_commission_detail_mod->find(
                    array("conditions"=>"account_id = ".$id)
                );
                if(!empty($commission_account_detailInfo)){
                    foreach($commission_account_detailInfo as $k=>$v){
                        $commission_account_detailInfo[$k]['order_pay_time_value'] = '未付款';
                        if($v['order_pay_time'] > 0){
                            $commission_account_detailInfo[$k]['order_pay_time_value'] = date('Y-m-d H:i:s',$v['order_pay_time']);
                        }
                        $commission_account_detailInfo[$k]['order_finish_time_value'] = '未完成';
                        if($v['order_finish_time'] > 0){
                            $commission_account_detailInfo[$k]['order_finish_time_value'] = date('Y-m-d H:i:s',$v['order_finish_time']);
                        }
                        $commission_account_detailInfo[$k]['commission_finish_time_value'] = date('Y-m-d H:i:s',$v['commission_finish_time']);
                        $commission_account_detailInfo[$k]['commission_proportion_value'] = $v['commission_proportion'] * 100;
                    }
                    $code = 1;
                    $msg = '查询成功';
                    $data = $commission_account_detailInfo;
                }
            }
        }
        echo json_encode(array('code'=>$code,'msg'=>$msg,'data'=>$data));
        exit();
    }


}

